package cn.bonoon.kernel.web.components;

import cn.bonoon.kernel.descriptors.UIPropertyAccessor;
import cn.bonoon.kernel.web.html.UIPropertyBuilder;
import cn.bonoon.kernel.web.html.form.ComponentPropertyParser;

public abstract class UIComponent extends AbstractInputComponent{

	/**
	 * 组件的类型名称，如：combobox,combotree等名称
	 */
	protected final String type;
	
	protected boolean defaultValue = true;
	
	protected UIComponent(String type, ComponentPropertyParser componentParser, UIPropertyAccessor accessor) {
		super(componentParser, accessor);
		this.type = type;
	}
	
	protected void initComponent(UIPropertyBuilder builder){ }
	
	@Override
	protected void parser(UIPropertyBuilder builder) throws Exception{
		initComponent(builder);
		function(builder);
		input(builder);
	}

	protected void function(UIPropertyBuilder builder) {
		StringBuilder script = builder.getInitBlock();
		script.append("jQuery('#").append(componentId).append("').").append(type).append("({disabled:false");
		initComponentAttribute(script, builder);
		script.append("});");
	}
	
	protected void initComponentAttribute(StringBuilder script, UIPropertyBuilder builder){ }
	
	protected void initInputAttribute(StringBuilder html, StringBuilder settings, UIPropertyBuilder builder){
		super.initInputAttribute(html, settings, builder);
		if (defaultValue) {
			html.append(" value='$!{").append(builder.getBindedName()).append('.').append(componentName).append("}'");
		}
	}
}
